package mkis.service.root.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoggingIntercptor implements HandlerInterceptor {
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object hanlder) throws Exception {
		
		String url=request.getRequestURL().toString();
		String cp=request.getContextPath();
		
		String fullURL=cp+url;
		
		logger.debug("===============request URL start : {}=================",fullURL);
		
		//iterator 돌릴 Enumeration
		Enumeration<String> paramNames = request.getParameterNames();
		
		//iterator 돌리기전 있는지 여부 체크 할 변수
		Enumeration<String> paramChk = paramNames;
		
		if(!paramChk.hasMoreElements()){
			logger.debug("\t --파라미터 정보 없음.");
		}else{
			while(paramNames.hasMoreElements()){
				String key=paramNames.nextElement();
				logger.info("\t request param : {}",request.getParameter(key));
			}
		}
		logger.debug("===============request URL   end : {}=================",fullURL);
		return true;
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) throws Exception {
		
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)	throws Exception {
		
	}

}
